package utildb;

public class IDGenerator {

	private int lastID_ = 0; //to avoid duplicate Ids 

	private static IDGenerator uniqueInstance;

	private IDGenerator() {
		super();
	}

	public synchronized int createID() {
		//because many databases do not support Java
		//long values convert everything to int
		int currID = (int) System.currentTimeMillis();
		while (currID == lastID_) {
			try {
				Thread.sleep(1);
			} catch (InterruptedException e) {
			}
			currID = (int) System.currentTimeMillis();
		}
		lastID_ = currID;
		return currID;
	}

	public static synchronized IDGenerator getUniqueInstance() {

		if (uniqueInstance == null) {
			uniqueInstance = new IDGenerator();
		}
		return uniqueInstance;

	}

}
